*************************************************************************************
* Figure 3: RD specifications - effect of earlier eligibility on voting in subsequent elections

use  "Data_temp//turnout.dta", clear

tab election, gen(elec_dummy)
drop elec_dummy1

* edit covariates
foreach var in female born_abroad mwork14 mum_lowedu white_brit  employed low_edu married urban england  scotland wales north_ireland birth_month {
	recode `var' (.=99)
	qui tab `var', gen(`var'_dummy)
	drop `var'_dummy1
	}
	
global covariates female_dummy2 born_abroad_dummy2 born_abroad_dummy3 mwork14_dummy2 mwork14_dummy3 mum_lowedu_dummy2 mum_lowedu_dummy3 white_brit_dummy2 white_brit_dummy3 employed_dummy2 employed_dummy3 low_edu_dummy2 low_edu_dummy3 married_dummy2 married_dummy3 urban_dummy2 urban_dummy3 england_dummy2 scotland_dummy2 wales_dummy2 north_ireland_dummy2	birth_month_dummy2 birth_month_dummy3 birth_month_dummy4 birth_month_dummy5 birth_month_dummy6 birth_month_dummy7 birth_month_dummy8 birth_month_dummy9 birth_month_dummy10 birth_month_dummy11 birth_month_dummy12



sum $covariates


**************************************************************
* t0-t3
gen rel_age=.
forvalues c = 0/3 {

	* Linear and quadratic
	rdrobust voted age`c' if age`c'!=0, covs(elec_dummy*) p(1)  h(24)
		global eff_obs=e(N_h_l)+e(N_h_r)
		regsave RD_Estimate using "Data_temp/election`c'_p1.dta", replace ci addlabel(elec, `c', poly, 1, contr, 0, eff_obs, $eff_obs)
	rdrobust voted age`c' if age`c'!=0, covs(elec_dummy*) p(2)  h(24)
		global eff_obs=e(N_h_l)+e(N_h_r)
		regsave RD_Estimate using "Data_temp/election`c'_p2.dta", replace ci addlabel(elec, `c', poly, 2, contr, 0, eff_obs, $eff_obs)
	rdrobust voted age`c' if age`c'!=0, covs(elec_dummy* $covariates) p(1)  h(24)
		global eff_obs=e(N_h_l)+e(N_h_r)
		regsave RD_Estimate using "Data_temp/election`c'_p3.dta", replace ci addlabel(elec, `c', poly, 1, contr, 1, eff_obs, $eff_obs)
	rdrobust voted age`c' if age`c'!=0, covs(elec_dummy* $covariates) p(2)  h(24)
		global eff_obs=e(N_h_l)+e(N_h_r)
		regsave RD_Estimate using "Data_temp/election`c'_p4.dta", replace ci addlabel(elec, `c', poly, 2, contr, 1, eff_obs, $eff_obs)
		
	* Bandwidth
	rdbwselect voted age`c' if age`c'!=0,  p(1)  	
		global bw=e(h_mserd)	
	rdrobust voted age`c' if age`c'!=0, covs(elec_dummy*) p(1)   h($bw)
		global eff_obs=e(N_h_l)+e(N_h_r)
		regsave RD_Estimate using "Data_temp/election`c'_p5.dta", replace ci addlabel(elec, `c', bw, 1, eff_obs, $eff_obs)
	rdrobust voted age`c' if age`c'!=0, covs(elec_dummy*) p(1)  h(48)
		global eff_obs=e(N_h_l)+e(N_h_r)
		regsave RD_Estimate using "Data_temp/election`c'_p6.dta", replace ci addlabel(elec, `c', bw, 2, eff_obs, $eff_obs)
	rdrobust voted age`c' if age`c'!=0, covs(elec_dummy*) p(1)  h(12)
		global eff_obs=e(N_h_l)+e(N_h_r)
		regsave RD_Estimate using "Data_temp/election`c'_p7.dta", replace ci addlabel(elec, `c', bw, 3, eff_obs, $eff_obs)
		}
		
		


		
		

**************************************************************
* pooled 

* duplicate observations going into either estimate
gen m1=1 if inrange(age1,-24,24) & inrange(age2,-24,24)		
tab m1
expand 2 if m1==1, gen(dupl12)
replace age2=. if dupl12==1 // ignore those from duplicate observations
replace age1=. if dupl12==0 & inrange(age2,-24,24) //
count if inrange(age1,-24,24) & inrange(age2,-24,24)

gen m2=1 if inrange(age2,-24,24) & inrange(age3,-24,24)		
expand 2 if m2==1, gen(dupl23)
replace age2=. if dupl23==1
replace age3=. if dupl23==0 & inrange(age2,-24,24) 
count if inrange(age2,-24,24) & inrange(age3,-24,24)

forvalues x = 1/3 {
		replace rel_age=age`x' if inrange(age`x',-24,24) & rel_age==.
		}

rdrobust voted rel_age if rel_age!=0, covs(elec_dummy*) p(1)  h(24)
	global eff_obs=e(N_h_l)+e(N_h_r)
	regsave RD_Estimate using "Data_temp/election_pooled_1.dta", replace ci addlabel(poly, 1, contr, 0, eff_obs, $eff_obs, elec, 4)
rdrobust voted rel_age if rel_age!=0, covs(elec_dummy*  $covariates) p(1)  h(24) 
	regsave RD_Estimate using "Data_temp/election_pooled_2.dta", replace ci addlabel(poly, 1, contr, 1, eff_obs, $eff_obs, elec, 4)
rdrobust voted rel_age if rel_age!=0, covs(elec_dummy*) p(2)  h(24)
	regsave RD_Estimate using "Data_temp/election_pooled_3.dta", replace ci addlabel(poly, 2, contr, 0, eff_obs, $eff_obs, elec, 4)
rdrobust voted rel_age if rel_age!=0, covs(elec_dummy* $covariates) p(2)  h(24) 
	regsave RD_Estimate using "Data_temp/election_pooled_4.dta", replace ci addlabel(poly, 2, contr, 1, eff_obs, $eff_obs, elec, 4)	
	
* Bandwidth
rdbwselect voted rel_age if rel_age!=0,  p(1)  	
	global bw=e(h_mserd)	
rdrobust voted rel_age if rel_age!=0, covs(elec_dummy*) p(1)  h($bw)
	global eff_obs=e(N_h_l)+e(N_h_r)
	regsave RD_Estimate using "Data_temp/election_pooled_5.dta", replace ci addlabel(bw, 1, eff_obs, $eff_obs, elec, 4)
rdrobust voted rel_age if rel_age!=0, covs(elec_dummy*) p(1) h(48) 
	global eff_obs=e(N_h_l)+e(N_h_r)
	regsave RD_Estimate using "Data_temp/election_pooled_6.dta", replace ci addlabel(bw, 2, eff_obs, $eff_obs, elec, 4)
rdrobust voted rel_age if rel_age!=0, covs(elec_dummy*) p(1) h(12)  
	global eff_obs=e(N_h_l)+e(N_h_r)
	regsave RD_Estimate using "Data_temp/election_pooled_7.dta", replace ci addlabel(bw, 3, eff_obs, $eff_obs, elec, 4)
	
	
*****************************************
* additional bandwidth checks
clear	
forvalues c=0/3 {
	forvalues p = 1/7 {
		append using "Data_temp/election`c'_p`p'.dta"
		}
	}
forvalues p = 1/7 {
		append using "Data_temp/election_pooled_`p'.dta"
		}
	
replace poly=3 if bw<.	
replace bw=0 if bw==.
sort elec contr poly bw	
by elec: gen n=_n	
	
replace elec=elec-0.3 if n==1
replace elec=elec-0.2 if n==2
replace elec=elec-0.1 if n==3
replace elec=elec+0.1 if n==5
replace elec=elec+0.2 if n==6
replace elec=elec+0.3 if n==7
	
	
twoway (rcap ci_lo ci_up elec, lcolor(gs8) lwidth(medthin)) ///
(scatter coef elec if n==1,  msize(medlarge) msymbol(D) mfcolor(white) mlwidth(medthick) mlcolor(black)) ///
(scatter coef elec if n==2, msize(medlarge) msymbol(O) mfcolor(white) mlwidth(medthick) mlcolor(edkblue)) ///
(scatter coef elec if n==3, msize(medlarge) msymbol(T) mfcolor(white) mlwidth(medthick) mlcolor(edkblue)) ///
(scatter coef elec if n==4, msymbol(S) mfcolor(white) mlwidth(medthick) mlcolor(edkblue)) ///
(scatter coef elec if n==5, msymbol(O) mfcolor(gs12) mlwidth(medthick) mlcolor(dknavy)) ///
(scatter coef elec if n==6, msymbol(T) mfcolor(gs12) mlwidth(medthick) mlcolor(dknavy)) ///
(scatter coef elec if n==7, msymbol(S) mfcolor(gs12) mlwidth(medthick) mlcolor(dknavy)), ///
yl(-.1(0.1)0.6) xsc(r(-0.5 4.5)) xt("") ///
xlabel(none) legend(order(2 "1 / - / 24" 3 "2 / - / 24" 4 "1 / Y / 24"  5 "2 / Y / 24" ///
6 "1 / - / optimal" 7 "1 / - / 48" 8 "1 / - / 12") row(2) size(small) ///
subtitle("Specification (polynomial order / controls / bandwidth in months)", size(medsmall)) ) ///
subt("")  yt("RD estimate") ///
 xlab(0 "t{subscript:0}" 1 "t{subscript:1}" 2 "t{subscript:2}" 3 "t{subscript:3}" 4 `""t{subscript:1}-t{subscript:3}" "pooled""', labsize(*1.2)) ///
 xline(3.5, lwidth(thin) lcolor(gs12))		yline(0, lwidth(thin) lcolor(gs12) lpattern(solid))  ///
 graphregion(margin(zero))
graph export "Figures/fg3.eps", replace

 